<template>
  <div class="view-fill">
    <div class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="turnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('putShelfTask:transfer') &&
              (infoList.data.status === 2 || infoList.data.status === 3)
            "
            @click="transfer"
            >转交</el-button
          >
          <el-button v-if="infoList.data.status === 1" @click="orderReceive"
            >接单</el-button
          >
          <el-button
            v-if="
              $permission('putShelfTask:cancel') &&
              (infoList.data.status === 2 ||
                infoList.data.status === 3 ||
                infoList.data.status === 6 ||
                infoList.data.status === 7)
            "
            @click="doCancel"
            >取消</el-button
          >
          <el-button
            v-if="
              $permission('putShelfTask:store') &&
              (infoList.data.onlyOnePendStockTask === 1 ||
                infoList.data.status === 7)
            "
            @click="doStore"
            >入库</el-button
          >
          <el-button
            v-if="
              $permission('putShelfTask:stop') && infoList.data.status === 3
            "
            @click="doStop"
            >中止</el-button
          >
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="canPrint"
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!-- <el-button
            v-if="$permission('putShelfTask:printOrderDetail')"
            @click="print"
            >打印</el-button
          > -->
          <el-button
            v-if="$permission('putShelfTask:excelOrderDetail')"
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList"></basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('detailInfo') }}</p>
          </div>
          <div class="model-panel-body">
            <div class="layui-card-body">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :check-box="false"
                :tab-index="true"
                :isDynamic="true"
              >
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <individual-details
      v-if="individualInfo.visible"
      v-model:show-dialog="individualInfo.visible"
      v-model:query="individualInfo.query"
      :info="individualInfo.info"
    />
    <select-users
      v-if="userInfo.visible"
      @callback="save"
      v-model:showDialog="userInfo.visible"
      :query="userInfo.query"
      :checkBox="false"
      :saveEmpty="false"
      :api="userInfo.api"
      :highlightRow="true"
    />
  </div>
</template>

<script setup name="putShelfTaskDetails">
import { getCustomizeSetting } from '@/api/customize'
import {
  transferPutShelfTask,
  excelPutShelfTaskOrder,
  getPutShelfTaskById,
  getPutShelfTaskDetailCustomizeListAll,
  printPutShelfTaskOrder,
  putShelfTaskCancel,
  putShelfTaskStore,
  putShelfTaskReceive,
  putShelfTaskStop,
} from '@/api/wms.js'
import { fatchTemplateByCode, getRrgUserPage } from '@/api/base'
import { orderOpAffairAll } from '@/api/affair'
import IndividualDetails from './individualDetails'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'

const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'tableId'])

const title = ref('上架任务详情')
const infoList = ref({
  data: {},
  fieldList: [
    {
      label: '任务单号',
      value: 'orderNo',
    },
    {
      label: '仓库名称',
      value: 'depotName',
    },
    {
      label: '耗材品类数',
      value: 'productQty',
    },
    {
      label: '耗材总数量',
      value: 'totalQty',
    },
    {
      label: '接单时间',
      value: 'receivingTime',
    },
    {
      label: '完成时间',
      value: 'completeTime',
    },
    {
      label: '状态',
      value: 'statusValue',
    },
    {
      label: '上架人',
      value: 'putShelfUserName',
    },
    {
      label: '创建人',
      value: 'createUserName',
    },
    {
      label: '创建时间',
      value: 'createTime',
    },
    {
      label: '备注',
      value: 'remark',
      type: 'popover',
    },
  ],
})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '个体详情',
        event: showDetail,
        show: proxy.$permission('putShelfTask:individualDetails'),
      },
    ],
  },
})
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.tableId,
      },
    },
    queryParams: {},
  },
})
const affairTableInfo = ref({
  api: orderOpAffairAll,
  query: {
    //数字字典单据类型 资产转移：74
    orderType: 74,
    orderId: props.tableId,
    sort: 'operate_time desc',
  },
})

const userInfo = ref({
  visible: false,
  api: getRrgUserPage,
  id: '',
  query: {
    idNotIn: '',
    depotAuth: '',
    status: 1,
    isTransfer: 1,
  },
})

const individualInfo = ref({
  visible: false,
  query: {},
  info: {},
})
const canPrint = computed(() => {
  return proxy.$permission('putShelfTask:printOrderDetail')
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

setData()
function setData() {
  let obj = {
    putQty: {
      label: '上架数量',
      value: 'putQty',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    putOnQty: {
      label: '已上架数量',
      value: 'putOnQty',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    notPutQty: {
      label: '未上架数量',
      value: 'notPutQty',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    locationTypeValue: {
      label: '货位属性',
      value: 'locationTypeValue',
      asValue: 'locationType',
      componentType: 'select-model',
      code: 'goods_location_type',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
}
//接单
function orderReceive() {
  putShelfTaskReceive(infoList.value.data).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function showDetail(row) {
  let ids
  if (row.groupConcatIds) {
    //未定义表示没有分组条件
    ids = row.groupConcatIds
  } else {
    ids = '' + row.id
  }
  individualInfo.value.visible = true
  individualInfo.value.info = row
  individualInfo.value.query = {
    id: {
      queryType: 'IN',
      queryCategory: 'AND',
      value: ids,
    },
    orderId: {
      queryType: 'EQUALS',
      queryCategory: 'AND',
      value: props.tableId,
    },
  }
}
function turnBack() {
  proxy.$emit('returnBack', false)
}
//转交
function transfer() {
  userInfo.value.visible = true
  userInfo.value.id = infoList.value.data.id
  userInfo.value.query.idNotIn = infoList.value.data.putShelfUser || ''
  userInfo.value.query.depotAuth = infoList.value.data.depotId
}
// 入库
function doStore() {
  proxy
    .$confirm('确认入库吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(() => {
      putShelfTaskStore(props.tableId).then((resp) => {
        if (resp && resp.success) {
          getMain()
          getList()
        }
      })
    })
    .catch(() => {})
}
//取消
function doCancel() {
  proxy
    .$confirm('确认取消吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(() => {
      putShelfTaskCancel(props.tableId).then((resp) => {
        if (resp && resp.success) {
          getMain()
          getList()
        }
      })
    })
    .catch(() => {})
}
// 中止
function doStop() {
  proxy
    .$confirm('确认中止吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(() => {
      putShelfTaskStop(props.tableId).then((resp) => {
        if (resp && resp.success) {
          getMain()
          getList()
        }
      })
    })
    .catch(() => {})
}
//转交保存
function save(data) {
  let params = {
    id: userInfo.value.id,
    putShelfUser: data.id,
  }
  transferPutShelfTask(params).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}

//导出excel
async function exportExcel() {
  excelPutShelfTaskOrder(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'wmsPutShelfTaskDetail',
    deptId: infoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: props.tableId,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    updatePrintCount({
      orderNo: infoList.value.data.orderNo,
      orderId: props.tableId,
      orderType: 64,
    })
  } catch (error) {}
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: props.tableId,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function getPrintData(temp) {
  let params = {
    tableKey:
      'putShelfTaskDetails-com.slip.spd.entity.vo.wms.WmsPutShelfTaskDetailVO',
    orderIds: [props.tableId],
  }
  printPutShelfTaskOrder(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
function getList() {
  getPutShelfTaskDetailCustomizeListAll(filterInfo.value.query).then((resp) => {
    if (resp && resp.success) {
      tableInfo.value.data = resp.data
    }
  })
}

function getMain() {
  getPutShelfTaskById(props.tableId).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
function returnModify() {
  getMain()
  getList()
}
function getCustomize() {
  let _tableInfo = tableInfo.value
  let param = {
    tableKey:
      'putShelfTaskDetails-com.slip.spd.entity.vo.wms.WmsPutShelfTaskDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(_tableInfo.fieldList, data.detail, true)
      _tableInfo.fieldList = obj.fieldList
      getMain()
      getList()
    }
  })
}
</script>

<style scoped></style>
